package net.tv90.bfmccore.util.file;

import net.tv90.bfmccore.util.vector.Vector3i;
import org.yaml.snakeyaml.Yaml;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static net.tv90.bfmccore.BFMCCore.PLUGIN_ROOT;

public class FileUtils {

    /**
     * 在文件中读取Vector3i列表
     * @param path 文件路径
     * @param listName 列表名
     * @return Vector3i列表
     */
    @SuppressWarnings("unchecked")
    public static List<Vector3i> readVector3iList(String path, String listName){
        try {
            File file = new File(PLUGIN_ROOT, path);
            Map<String, List<String>> map = (Map<String, List<String>>) (new Yaml()).loadAs(Files.newInputStream(file.toPath()), Map.class);
            List<String> list = map.get(listName);

            List<Vector3i> vector3iList = new ArrayList<>();
            for (String s : list) {
                String[] split = s.split(",");
                vector3iList.add(new Vector3i(Integer.parseInt(split[0]), Integer.parseInt(split[1]), Integer.parseInt(split[2])));
            }
            return vector3iList;
        } catch (Exception e) {
            System.out.println("在文件中读取Vector3i列表错误，找不到" + path + "中的" + listName + "列表");
        }
        return new ArrayList<>();
    }

    /**
     * 文件读写功能
     * @param file 文件
     * @param content 内容
     * @throws Exception 异常
     */
    public static void writeAllText(File file, String content) throws Exception {
        try (Writer writer = new OutputStreamWriter(Files.newOutputStream(file.toPath()), StandardCharsets.UTF_8)) {
            writer.write(content);
        }
    }

    /**
     * 文件读写功能, 追加模式
     * @param file 文件
     * @param content 内容
     * @throws Exception
     */
    public static void writeAllTextAsAppendMode(File file, String content) throws Exception {
        try (Writer writer = new OutputStreamWriter(Files.newOutputStream(file.toPath(), StandardOpenOption.APPEND, StandardOpenOption.CREATE), StandardCharsets.UTF_8)) {
            writer.write(content);
        }
    }

    /**
     * 读取文件所有内容
     * @param file 文件
     * @return 内容
     */
    public static String readAllText(File file) throws IOException {
        Path path = Paths.get(file.toURI());
        byte[] bytes = Files.readAllBytes(path);
        return new String(bytes);
    }


    /**
     * 复制文件夹
     * @param src 源文件夹
     * @param target 目标文件夹
     */
    public static void copyDir(File src, File target) {
        if (src.isDirectory()) {
            target.mkdirs();
            File[] files = src.listFiles();
            if (files != null) {
                for (File file : files) {
                    copyDir(file, new File(target, file.getName()));
                }
            }
        } else {
            try {
                copyFile(src, target);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
       // System.out.println("复制成功1个文件夹");
    }

    /**
     * 复制文件
     * @param src 源文件
     * @param target 目标文件
     * @throws IOException
     */
    public static void copyFile(File src, File target) throws IOException {
        Files.copy(src.toPath(), target.toPath(), StandardCopyOption.REPLACE_EXISTING);
       // System.out.println("复制成功1个文件");
    }
}
